﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;

using Marking.Dashboard.ViewModels.Stock;
using Marking.Dashboard.Services.StockProxies;

namespace Marking.Dashboard.Services.Remotes
{
    public class RemoteStockService : IStockService
    {
        public void LoadSalesClothStockInfos(Action<IEnumerable<StockInfo>> action)
        {
            var proxy = new StockServiceClient();

            proxy.LoadSalesStockInfosCompleted += (sender, e) =>
            {
                this.ProcessResults(action, e.Result);
            };

            proxy.LoadSalesStockInfosAsync();
        }

        public void LoadColouredClothStockInfos(Action<IEnumerable<StockInfo>> action)
        {
            var proxy = new StockServiceClient();

            proxy.LoadColouredClothStockInfosCompleted += (sender, e) =>
            {
                this.ProcessResults(action, e.Result);
            };

            proxy.LoadColouredClothStockInfosAsync();
        }

        public void LoadClothStockInfos(Action<IEnumerable<StockInfo>> action)
        {
            var proxy = new StockServiceClient();

            proxy.LoadClothStockInfosCompleted += (sender, e) =>
            {
                this.ProcessResults(action, e.Result);
            };

            proxy.LoadClothStockInfosAsync();
        }

        private void ProcessResults(Action<IEnumerable<StockInfo>> action, IEnumerable<StockDTO> results)
        {
            action(results.Select(x => new StockInfo
            {
                WarehouseId = x.WarehouseId,
                Warehouse = x.Warehouse,
                Quantity = x.Quantity > 0 ? x.Quantity : 0
            }));
        }
    }
}
